package com.maochang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maochang.mapper.*;
import com.maochang.pojo.*;
import com.maochang.pojo.util.ChuAnSuoKuAng;
import com.maochang.pojo.util.ZhuRenWUHeXunJianMingXi;
import com.maochang.service.XunJianRenWuZhiDingYuFenPeiService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service("XunJianRenWuZhiDingYuFenPeiServicelmpl")
public class XunJianRenWuZhiDingYuFenPeiServicelmpl extends ServiceImpl<PsInspectiontaskMainMapper, PsInspectiontaskMain> implements XunJianRenWuZhiDingYuFenPeiService {

    @Resource
    private   PsInspectiontaskMainMapper mainMapper;

    @Resource
    private PsInspectorDetailMapper psInspectorDetailMapper;

    @Resource
    private PsLineInfoMapper psLineInfoMapper;

    @Resource
    private PsPoleInfoMapper psPoleInfoMapper;
    @Resource
    private PsUserMapper psUserMapper;

    @Override
    public Page<PsInspectiontaskMain> ChaXjZhuBiAo(PsInspectiontaskMain ps,int page,int size){
        Page<PsInspectiontaskMain> psliPage=new Page<>(page,size);
        QueryWrapper<PsInspectiontaskMain> wrapper= Wrappers.query();
        wrapper.like(!StringUtils.isBlank(ps.getInspectionTaskCode()),"inspectionTaskCode",ps.getInspectionTaskCode())
                .like(!StringUtils.isBlank(ps.getLineName()),"lineName",ps.getLineName())
                .eq(ps.getTaskStatus()!=null,"taskStatus",ps.getTaskStatus())
                .eq(!StringUtils.isBlank(ps.getIssuedByName()),"issuedByName",ps.getIssuedByName())
                .ge(!StringUtils.isBlank(ps.getIssuedByName()),"finishTime",ps.getFinishTime())
                .le(!StringUtils.isBlank(ps.getIssuedByName()),"creationTime",ps.getCreationTime());
      return mainMapper.AllPXjList(psliPage,wrapper);
    }
    @Override
    public Boolean gaiZhuAngTai(Integer id) {
        QueryWrapper<PsInspectiontaskMain> wrapper= Wrappers.query();
        PsInspectiontaskMain psInspectiontaskMain=new PsInspectiontaskMain();
        psInspectiontaskMain.setTaskStatus(32);
        psInspectiontaskMain.setTaskStatusName("已分配");
        wrapper.eq("id",id);
        int aa=  mainMapper.update(psInspectiontaskMain,wrapper);
        boolean fAlg=false;
        if(aa>0){
            fAlg=true;
        }
        return fAlg;
    }

    @Override
    public Boolean QuXiAoXunJiAn(Integer id) {
        QueryWrapper<PsInspectiontaskMain> wrapper= Wrappers.query();
        PsInspectiontaskMain psInspectiontaskMain=new PsInspectiontaskMain();
        psInspectiontaskMain.setIsFouQuXiao("1");
        wrapper.eq("id",id);
        int aa=  mainMapper.update(psInspectiontaskMain,wrapper);
        boolean fAlg=false;
        if(aa>0){
            fAlg=true;
        }
        return fAlg;
    }

    @Override
    public Boolean addXunJiAn(ZhuRenWUHeXunJianMingXi xi, HttpSession httpSession) {
        PsInspectorDetail psInspectorDetail= new PsInspectorDetail();
        PsUser  yongHu=null;
        int  row=0;
          //插入信息到 巡检员明细表
            //3.主表插入
            //巡检任务编号
                  //自动赋值了
            //巡检任务名称
                  //自动赋值了
            //巡检线路id
                //自动赋值
                PsLineInfo aa= psLineInfoMapper.selectById(xi.getPsInspectiontaskMain().getLineId());
                //所属线路code
                xi.getPsInspectiontaskMain().setLineCode(aa.getLineCode());
                //所属线路name
                xi.getPsInspectiontaskMain().setLineName(aa.getLineName());
            //起始杆号id
                //自动赋值
                  PsPoleInfo aa1=psPoleInfoMapper.selectById(xi.getPsInspectiontaskMain().getStartPole());
                 xi.getPsInspectiontaskMain().setStartPoleCode(aa1.getPoleCode());
            //终止杆号id
                //自动赋值
            //System.out.println("年后哈DNFsad发的发送到付款了就爱上发");
                  PsPoleInfo aa2=psPoleInfoMapper.selectById(xi.getPsInspectiontaskMain().getEndPole());
                  System.out.println(aa2.toString()+"aa2的值");
                  System.out.println(aa2.getPoleCode()+"ccccc");
                  xi.getPsInspectiontaskMain().setEndPoleCode(aa2.getPoleCode());
            //下发人name
                //下发人name 前端赋值
                QueryWrapper<PsUser> wrapper= Wrappers.query();
                wrapper.eq("userName",xi.getPsInspectiontaskMain().getIssuedByName());
                PsUser psUser=psUserMapper.selectOne(wrapper);
                //下发人 code
                xi.getPsInspectiontaskMain().setIssuedByCode(psUser.getUserCode());
            //任务状态id

            //任务完成时间
                 LocalDateTime nowTime= LocalDateTime.now();
                 xi.getPsInspectiontaskMain().setFinishTime(nowTime);
            //创建时间
                //创建时间前端赋值了
            //是否取消
                xi.getPsInspectiontaskMain().setIsFouQuXiao("0");
            //创建人
                //自动赋值了
            //最后修改时间
                 LocalDateTime chaungJian=LocalDateTime.now();
                 xi.getPsInspectiontaskMain().setCreationTime(chaungJian);
            //备注
                //前端赋值
        if(xi.getPsInspectorDetail()!=null){
            xi.getPsInspectiontaskMain().setTaskStatus(32);
            //任务状态name
            xi.getPsInspectiontaskMain().setTaskStatusName("已分配");
        }else {
          //巡检员列表等于空的话
            xi.getPsInspectiontaskMain().setTaskStatus(31);
            xi.getPsInspectiontaskMain().setTaskStatusName("待分配");
        }
        int  a=mainMapper.insert(xi.getPsInspectiontaskMain());
        boolean fAlg=false;
        if(a>0){
            //2.副表插入
            if(xi.getPsInspectorDetail()!=null) {
                Integer RenWuID = xi.getPsInspectiontaskMain().getId();
                //System.out.println(RenWuID + "任务id");
                for (ChuAnSuoKuAng chuAnSuoKuAng : xi.getPsInspectorDetail()) {
                    //主任务id
                    psInspectorDetail.setTaskId(RenWuID);
                    //巡检员的code
                    QueryWrapper<PsUser> wrapperxj = Wrappers.query();
                    wrapperxj.eq("id", chuAnSuoKuAng.getValue());
                    PsUser user = psUserMapper.selectOne(wrapperxj);
                    psInspectorDetail.setInspectorCode(user.getUserCode());
                    //巡检员 name
                    psInspectorDetail.setInspectorName(chuAnSuoKuAng.getTitle());
                    //创建者
                    yongHu = (PsUser) httpSession.getAttribute("loginUser");
                    psInspectorDetail.setCreatedBy(yongHu.getUserName());
                    //创建时间
                    psInspectorDetail.setCreationTime(nowTime);
                    //最后修改时间
                    psInspectorDetail.setLastUpdateTime(nowTime);
                    psInspectorDetailMapper.insert(psInspectorDetail);
                    row++;
                }
            };
            fAlg=true;
        }
        return fAlg;
    }

    /**
     *  查询某个巡检任务信息
     * @param id
     * @return
     */
    @Override
    public PsInspectiontaskMain ChaRenWu(String id) {
        QueryWrapper<PsInspectiontaskMain> wrapper= Wrappers.query();
        wrapper.eq("id",id);
        return mainMapper.selectOne(wrapper);
    }
    /**
     * 查询某个任务的巡检员
     * @param RenWuID
     * @return
     */
    @Override
    public List<PsInspectorDetail> CaXJiAnYUn(String RenWuID) {
        QueryWrapper<PsInspectorDetail> wrapper=Wrappers.query();
        wrapper.eq("taskId",RenWuID);
        return psInspectorDetailMapper.selectList(wrapper);
    }

}
